package com.itheima.reflect6;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class MyReflectDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        //获取配置文件信息
        //创建Properties对象
        Properties prop = new Properties();
        //将配置文件信息加载到prop集合中
        prop.load(new BufferedReader(new FileReader("myreflect\\prop.properties")));
        System.out.println(prop);
        //获取配置文件信息
        String classname = (String) prop.get("class");
        String methodname = (String) prop.get("method");
        //通过反射获取class对象
        Class clazz = Class.forName(classname);
        //通过反射获取构造方法并创建对象
        Constructor con = clazz.getDeclaredConstructor();
        Object o = con.newInstance();
        System.out.println(o);
        //通过反射调用方法
        //获取方法对象并调用
        Method declaredMethod = clazz.getDeclaredMethod(methodname);
        declaredMethod.setAccessible(true);
        declaredMethod.invoke(o);

    }
}
